﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    [Serializable]
    public class D_FIM_MAS_Contacts : IDAL_FIM_MAS_Contacts
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        //

        public D_FIM_MAS_Contacts()
        {

        }

        public IList<M_FIM_MAS_Contacts> GetFull()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_Contacts> infoList = new List<M_FIM_MAS_Contacts>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT c.contact_seq, c.contact_name, c.seller_seq, c.contact_type"
                    + ", c.tax_id, c.citizen_id, c.is_juristic, c.tax_deduct, c.address, c.district_seq"
                    + ", c.country_seq, c.postal_code, c.tel, c.fax, c.email, c.contact_status"

                    + ", d.district_name, a.amp_name, p.prov_name, cou.country_name"
                    + ", concat(d.district_name, ' | ', a.amp_name, ' | ', p.prov_name) as district_text"
                    + ", case when c.contact_status = 'A' then 'ติดต่อได้'"
                        + " else case when c.contact_status = 'B' then 'Black lists'"
                            + " else 'ไม่สามารถติดต่อได้'"
                            + " end"
                        + " end as contact_status_name"
                    + ", case when c.contact_type = '1' then 'ส่วนราชการ'"
                        + " else case when c.contact_type = '2' then 'รัฐวิสาหกิจ'"
                            + " else case when c.contact_type = '3' then 'นิติบุคคล'"
                                + " else 'บุคคลธรรมดา'"
                                + " end"
                            + " end"
                        + " end as contact_type_name"
                    + ", case when c.contact_type = '1' or c.contact_type = '2' then 'ภาครัฐ'"
                        + " else 'ภาคเอกชน'"
                        + " end as contact_type_group_name"

                    + ", c.record_status, rs.record_status_name"
                    + ", c.created_by, c.created_when, c.updated_by, c.updated_when"
                    + " FROM fim_mas_contacts c"
                    + " LEFT JOIN mas_districts d ON c.district_seq = d.district_seq"
                    + " LEFT JOIN mas_amphurs a on d.amp_seq = a.amp_seq"
                    + " LEFT JOIN mas_provinces p on a.prov_seq = p.prov_seq"
                    + " LEFT JOIN mas_regions r on p.region_seq = r.region_seQ"
                    + " LEFT JOIN mas_countries cou on c.country_seq = cou.country_seq"
                    + " LEFT JOIN sys_record_status rs on c.record_status = rs.record_status"
                    + " WHERE c.record_status not like @delete_status"
                    + " ORDER BY c.contact_name";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();
                        info.contact_seq = Convert.ToInt32(drc[i]["contact_seq"]);
                        info.contact_name = drc[i]["contact_name"].ToString();
                        info.seller_seq = cn.ConvertNullInt(drc[i]["seller_seq"]);
                        info.contact_type = drc[i]["contact_type"].ToString();
                        info.tax_id = drc[i]["tax_id"].ToString();
                        info.citizen_id = drc[i]["citizen_id"].ToString();
                        info.is_juristic = drc[i]["is_juristic"].ToString();
                        info.tax_deduct = drc[i]["tax_deduct"].ToString();
                        info.address = drc[i]["address"].ToString();
                        info.district_seq = cn.ConvertNullInt(drc[i]["district_seq"]);
                        info.country_seq = cn.ConvertNullInt(drc[i]["country_seq"]);
                        info.postal_code = drc[i]["postal_code"].ToString();
                        info.tel = drc[i]["tel"].ToString();
                        info.fax = drc[i]["fax"].ToString();
                        info.email = drc[i]["email"].ToString();
                        info.contact_status = drc[i]["contact_status"].ToString();
                        info.district_name = drc[i]["district_name"].ToString();
                        info.amp_name = drc[i]["amp_name"].ToString();
                        info.prov_name = drc[i]["prov_name"].ToString();
                        info.country_name = drc[i]["country_name"].ToString();
                        info.district_text = drc[i]["district_text"].ToString();
                        info.contact_status_name = drc[i]["contact_status_name"].ToString();
                        info.contact_type_name = drc[i]["contact_type_name"].ToString();
                        info.contact_type_group_name = drc[i]["contact_type_group_name"].ToString();

                        info.record_status = drc[i]["record_status"].ToString();
                        info.record_status_name = drc[i]["record_status_name"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_Contacts> GetList(string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_Contacts> infoList = new List<M_FIM_MAS_Contacts>();

            if (filter == null)
                filter = "";

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@filter", "%" + filter + "%");

                strSQL = "SELECT c.ConId, c.NameTh"
                    + " FROM IcmMasContact c"
                    + " WHERE c.RecordStatus not like @delete_status"
                    + (filter.Length > 0 ? " AND c.NameTh like @filter" : "")
                    + " ORDER BY NameTh"; 

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();
                        info.contact_seq = Convert.ToInt32(drc[i]["ConId"]);
                        info.contact_name = drc[i]["NameTh"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_Contacts> GetListOffer(Nullable<int> icm_bid_seq, Nullable<int> contact_seq
            , string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_Contacts> infoList = new List<M_FIM_MAS_Contacts>();

            if (filter == null)
                filter = "";

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@icm_bid_seq", icm_bid_seq);
                dbManager.AddParameters(2, "@contact_seq", contact_seq);
                dbManager.AddParameters(3, "@filter", "%" + filter + "%");

                strSQL = "SELECT c.ConId, c.NameTh"
                    + " FROM  IcmBidder bo  "
                    + " LEFT JOIN IcmMasContact c ON bo.ConId = c.ConId"

                    + " WHERE c.RecordStatus not like @delete_status"
                    + " AND bo.BidderId = @icm_bid_seq"
                    + (contact_seq != null ? " AND c.ConId = @contact_seq" : "")
                    + " AND c.NameTh like @filter"
                    + " AND bo.OfferStatus = '0'"
                    + " ORDER BY c.NameTh";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();
                        info.contact_seq = Convert.ToInt32(drc[i]["ConId"]);
                        info.contact_name = drc[i]["NameTh"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_Contacts> GetListAR(string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_Contacts> infoList = new List<M_FIM_MAS_Contacts>();

            if (filter == null)
                filter = "";

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@filter", "%" + filter + "%");

                strSQL = "SELECT c.ConId, c.NameTh"
                    + " FROM IcmMasContact c"
                    + " WHERE c.RecordStatus not like @delete_status"
                    + (filter.Length > 0 ? " AND c.NameTh like @filter" : "")
                    + " ORDER BY NameTh";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();
                        info.contact_seq = Convert.ToInt32(drc[i]["ConId"]);
                        info.contact_name = drc[i]["NameTh"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FIM_MAS_Contacts GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@delete_status", SystemInfo._DeleteRecordStatus);


                //strSQL = "SELECT c.ConId, c.NameTh, c.seller_seq, c.contact_type"
                //    + ", c.tax_id, c.citizen_id, c.is_juristic, c.tax_deduct, c.address, c.district_seq"
                //    + ", c.country_seq, c.postal_code, c.tel, c.fax, c.email, c.contact_status"

                //    + ", d.district_name, a.amp_name, p.prov_name, cou.country_name"
                //    + ", concat(d.district_name, ' ', a.amp_name, ' ', p.prov_name) as district_text"
                //    + ", case when c.contact_status = 'A' then 'ติดต่อได้'"
                //        + " else case when c.contact_status = 'B' then 'Black lists'"
                //            + " else 'ไม่สามารถติดต่อได้'"
                //            + " end"
                //        + " end as contact_status_name"
                //    + ", case when c.contact_type = '1' then 'ส่วนราชการ'"
                //        + " else case when c.contact_type = '2' then 'รัฐวิสาหกิจ'"
                //            + " else case when c.contact_type = '3' then 'นิติบุคคล'"
                //                + " else 'บุคคลธรรมดา'"
                //                + " end"
                //            + " end"
                //        + " end as contact_type_name"
                //    + ", case when c.contact_type = '1' or c.contact_type = '2' then 'ภาครัฐ'"
                //        + " else 'ภาคเอกชน'"
                //        + " end as contact_type_group_name"

                //    + ", c.record_status, rs.record_status_name"
                //    + ", c.created_by, c.created_when, c.updated_by, c.updated_when"
                //    + " FROM IcmMasContact c"
                //    + " LEFT JOIN mas_districts d ON c.district_seq = d.district_seq"
                //    + " LEFT JOIN mas_amphurs a on d.amp_seq = a.amp_seq"
                //    + " LEFT JOIN mas_provinces p on a.prov_seq = p.prov_seq"
                //    + " LEFT JOIN mas_regions r on p.region_seq = r.region_seQ"
                //    + " LEFT JOIN mas_countries cou on c.country_seq = cou.country_seq"
                //    + " LEFT JOIN sys_record_status rs on c.record_status = rs.record_status"
                //    + " WHERE c.contact_seq = @seq";

                strSQL = "SELECT c.ConId, c.NameTh, c.NameEng, c.ConCategory"
                    + ", c.GfmisCode,c.Coordinator, c.VatTypeId,lp.LookUpName, c.RecordStatus"
                        +" FROM IcmMasContact c "
                        +" LEFT JOIN IcmMasLookUp lp ON c.VatTypeId = lp.LookUpId "
                        + " WHERE c.RecordStatus not LIKE @delete_status AND c.ConId = @seq";


                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.contact_seq = Convert.ToInt32(drc[0]["ConId"]);
                    info.contact_name = drc[0]["NameTh"].ToString();
                    info.contact_type = drc[0]["ConCategory"].ToString();
                    info.record_status = drc[0]["RecordStatus"].ToString();
                    //info.tax_id = drc[0]["tax_id"].ToString();
                    //info.contact_type_group_name = drc[0]["contact_type_group_name"].ToString();
                    //info.citizen_id = drc[0]["citizen_id"].ToString();
                    //info.is_juristic = drc[0]["is_juristic"].ToString();
                    //info.tax_deduct = drc[0]["tax_deduct"].ToString();
                    //info.address = drc[0]["address"].ToString();
                    //info.district_seq = cn.ConvertNullInt(drc[0]["district_seq"]);
                    //info.country_seq = cn.ConvertNullInt(drc[0]["country_seq"]);
                    //info.postal_code = drc[0]["postal_code"].ToString();
                    //info.tel = drc[0]["tel"].ToString();
                    //info.fax = drc[0]["fax"].ToString();
                    //info.email = drc[0]["email"].ToString();
                    //info.contact_status = drc[0]["contact_status"].ToString();
                    //info.district_name = drc[0]["district_name"].ToString();
                    //info.amp_name = drc[0]["amp_name"].ToString();
                    //info.prov_name = drc[0]["prov_name"].ToString();
                    //info.country_name = drc[0]["country_name"].ToString();
                    //info.district_text = drc[0]["district_text"].ToString();
                    //info.contact_status_name = drc[0]["contact_status_name"].ToString();
                    //info.contact_type_name = drc[0]["contact_type_name"].ToString();
                    //info.contact_type_group_name = drc[0]["contact_type_group_name"].ToString();


                    //info.record_status_name = drc[0]["record_status_name"].ToString();
                    //info.created_by = cn.ConvertNullInt(drc[0]["CreatedBy"]);
                    //info.created_when = cn.ConvertNullDateTime(drc[0]["CreatedWhen"]);
                    //info.updated_by = cn.ConvertNullInt(drc[0]["UpdatedBy"]);
                    //info.updated_when = cn.ConvertNullDateTime(drc[0]["UpdatedWhen"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public M_FIM_MAS_Contacts GetAddressInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);
                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@delete_status", SystemInfo._DeleteRecordStatus);

                /*
                strSQL = "SELECT c.ConId,c.ConCategory"
                    + ",case c.ConGrpId when 47 then 'บุคคล' else 'นิติบุคคล' end as contact_type_group_name"
                    +",c.TaxCode as tax_id,c.ConStatusId"
                    + ",Concat(c.Address,''"
                    + ",case when c.Village!='' then Concat(' หมู่',c.Village) else '' end"
                    + ",case when c.Building!='' then Concat(' อาคาร',c.Building) else '' end"
                    + ",case when c.Floor!='' then Concat(' ชั้น',c.Floor) else '' end"
                    + ",case when c.Lane!='' then Concat(' ตรอก/ซอย',c.Lane) else '' end"
                    + ",case when c.Road!='' then Concat(' ถนน',c.Road) else '' end"
                    + ",case when c.District != '' then case when c.Province!='กรุงเทพมหานคร' and c.Province!='กรุงเทพฯ' then Concat(' ตำบล',c.District) else Concat(' แขวง',c.District) end else '' end,'',case when c.Amphur != '' then case when c.Province!='กรุงเทพมหานคร' and c.Province!='กรุงเทพฯ' then Concat(' อำเภอ',c.Amphur) else Concat(' เขต',c.Amphur) end else '' end,''"
                    + ",case when c.Province!='' then case when c.Province!='กรุงเทพมหานคร' and c.Province!='กรุงเทพฯ' then Concat(' จังหวัด',c.Province) else Concat(' ',c.Province) end else '' end"
                    //+ ",' ',c.Country" comment out by A.Raksakaew
                    + ",' '"
                    + ",case when c.Postal!='' then Concat(' ',c.Postal) else '' end) as addr"
                    + ",case when c.NameTh != '' then NameTh else NameEng end as Name"
                    + " FROM IcmMasContact c"
                    //+ " LEFT JOIN IcmMasContactPartner cp ON c.ConId = cp.ConId"
                    //+ " WHERE c.RecordStatus not LIKE @delete_status  AND cp.RecordStatus not like @delete_status"                    
                    + " WHERE c.RecordStatus not LIKE @delete_status"
                    + " AND c.ConId = @seq";
                */

                //เปลี่ยนคิวรี่เนื่องจากข้อมูลที่อยู่ของ สรพ. มี ต.,อ.,จ. ปนอยู่ด้านหน้าข้อมูล ตำบล อำเภอ จังหวัด ถ้าใช้ตัวเดิมจะขึ้นเป็น ตำบลต. อำเภออ. เป็นต้น
                strSQL = "SELECT	c.ConId,c.ConCategory,case c.ConGrpId when 47 then 'บุคคล' else 'นิติบุคคล' end as contact_type_group_name"
                    + ",c.TaxCode as tax_id,c.ConStatusId,c.Address,c.Village,c.Lane,c.road,c.District,c.Amphur,c.Province,c.Postal"
		            +",case when c.NameTh != '' then NameTh else NameEng end as Name"
                    +" FROM	IcmMasContact c"
                    //+ " LEFT JOIN IcmMasContactPartner cp ON c.ConId = cp.ConId"
                    //+ " WHERE c.RecordStatus not LIKE @delete_status  AND cp.RecordStatus not like @delete_status"                    
                    + " WHERE c.RecordStatus not LIKE @delete_status"
                    + " AND c.ConId = @seq";


                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.contact_seq = Convert.ToInt32(drc[0]["ConId"]);
                    info.contact_name = drc[0]["Name"].ToString();
                    info.address = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", drc[0]["Address"].ToString()
                                                 , drc[0]["Village"].ToString().Trim() == "" ? "" : chkVillage(drc[0]["Address"].ToString(), drc[0]["Village"].ToString().Trim())
                                                 , drc[0]["Lane"].ToString().Trim() == "" ? "" : chkLane(drc[0]["Address"].ToString(), drc[0]["Lane"].ToString().Trim())
                                                 , drc[0]["road"].ToString().Trim() == "" ? "" : chkRoad(drc[0]["Address"].ToString(), drc[0]["road"].ToString().Trim())
                                                 , drc[0]["District"].ToString().Trim() == "" ? "" : chkDistrict(drc[0]["Province"].ToString(), drc[0]["District"].ToString().Trim())
                                                 , drc[0]["Amphur"].ToString().Trim() == "" ? "" : chkAmphur(drc[0]["Province"].ToString(), drc[0]["Amphur"].ToString().Trim())
                                                 , drc[0]["Province"].ToString().Trim() == "" ? "" : chkProvince(drc[0]["Province"].ToString())
                                                 , drc[0]["Postal"].ToString().Trim() == "" ? "" : string.Format(" {0}",drc[0]["Postal"].ToString())
                                                 //, drc[0]["tax_id"].ToString().Trim() == "" ? "" : string.Format("เลขประจำตัวผู้เสียภาษี {0}", drc[0]["tax_id"].ToString())
                                                );
                    //info.citizen_id = drc[0]["CitizenId"].ToString();
                    //info.tax_id = drc[0]["tax_id"].ToString(); // แก้ปัญหาข้อมูล Tax Id ไม่ขึ้น 06/01/58 
                    info.contact_type_group_name = drc[0]["contact_type_group_name"].ToString();
                    info.tax_id = string.IsNullOrEmpty(drc[0]["tax_id"].ToString().Trim()) ? "" : drc[0]["tax_id"].ToString().Trim();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_FIM_MAS_Contacts info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(19);

                dbManager.AddParameters(0, "@contact_seq", info.contact_seq);
                dbManager.AddParameters(1, "@contact_name", info.contact_name);
                dbManager.AddParameters(2, "@seller_seq", info.seller_seq);
                dbManager.AddParameters(3, "@contact_type", info.contact_type);
                dbManager.AddParameters(4, "@tax_id", info.tax_id);
                dbManager.AddParameters(5, "@citizen_id", info.citizen_id);
                dbManager.AddParameters(6, "@is_juristic", info.is_juristic);
                dbManager.AddParameters(7, "@tax_deduct", info.tax_deduct);
                dbManager.AddParameters(8, "@address", info.address);
                dbManager.AddParameters(9, "@district_seq", info.district_seq);
                dbManager.AddParameters(10, "@country_seq", info.country_seq);
                dbManager.AddParameters(11, "@postal_code", info.postal_code);
                dbManager.AddParameters(12, "@tel", info.tel);
                dbManager.AddParameters(13, "@fax", info.fax);
                dbManager.AddParameters(14, "@email", info.email);
                dbManager.AddParameters(15, "@contact_status", info.contact_status);

                dbManager.AddParameters(16, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(17, "@created_by", info.created_by);
                dbManager.AddParameters(18, "@created_when", info.created_when);

                strSQL = "INSERT INTO fim_mas_contacts"
                    + "(contact_name, seller_seq, contact_type, tax_id, citizen_id"
                    + ", is_juristic, tax_deduct, address, district_seq, country_seq"
                    + ", postal_code, tel, fax, email, contact_status"
                    + ", record_status, created_by, created_when)"
                    + " VALUES (@contact_name"
                        + ", @seller_seq"
                        + ", @contact_type"
                        + ", @tax_id"
                        + ", @citizen_id"
                        + ", @is_juristic"
                        + ", @tax_deduct"
                        + ", @address"
                        + ", @district_seq"
                        + ", @country_seq"
                        + ", @postal_code"
                        + ", @tel"
                        + ", @fax"
                        + ", @email"
                        + ", @contact_status"
                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when"
                        + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_FIM_MAS_Contacts info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(19);

                dbManager.AddParameters(0, "@contact_seq", info.contact_seq);
                dbManager.AddParameters(1, "@contact_name", info.contact_name);
                dbManager.AddParameters(2, "@seller_seq", info.seller_seq);
                dbManager.AddParameters(3, "@contact_type", info.contact_type);
                dbManager.AddParameters(4, "@tax_id", info.tax_id);
                dbManager.AddParameters(5, "@citizen_id", info.citizen_id);
                dbManager.AddParameters(6, "@is_juristic", info.is_juristic);
                dbManager.AddParameters(7, "@tax_deduct", info.tax_deduct);
                dbManager.AddParameters(8, "@address", info.address);
                dbManager.AddParameters(9, "@district_seq", info.district_seq);
                dbManager.AddParameters(10, "@country_seq", info.country_seq);
                dbManager.AddParameters(11, "@postal_code", info.postal_code);
                dbManager.AddParameters(12, "@tel", info.tel);
                dbManager.AddParameters(13, "@fax", info.fax);
                dbManager.AddParameters(14, "@email", info.email);
                dbManager.AddParameters(15, "@contact_status", info.contact_status);

                dbManager.AddParameters(16, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(17, "@updated_by", info.updated_by);
                dbManager.AddParameters(18, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_contacts"
                    + " SET contact_name = @contact_name"
                    + ", contact_type = @contact_type"
                    + ", tax_id = @tax_id"
                    + ", citizen_id = @citizen_id"
                    + ", is_juristic = @is_juristic"
                    + ", tax_deduct = @tax_deduct"
                    + ", address = @address"
                    + ", district_seq = @district_seq"
                    + ", country_seq = @country_seq"
                    + ", postal_code = @postal_code"
                    + ", tel = @tel"
                    + ", fax = @fax"
                    + ", email = @email"
                    + ", contact_status = @contact_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE contact_seq = @contact_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_FIM_MAS_Contacts info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@contact_seq", info.contact_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_contacts"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE contact_seq = @contact_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }


        #region Check Address Data

        public string chkVillage(string Addr,string Vill)
        {
            string ans = string.Empty;

            if (Addr.Contains("หมู่บ้าน") || Addr.Contains("ม."))
            {
                ans = "";
            }
            else
            {
                if (Vill.Contains("หมู่บ้าน") || Vill.Contains("ม."))
                {
                    ans = string.Format(" {0}",Vill);
                }
                else
                {
                    int moo = 0;
                    if (int.TryParse(Vill, out moo))
                    {
                        ans = string.Format(" ม.{0}", Vill);
                    }
                    else
                    {
                        ans = string.Format(" หมู่บ้าน{0}", Vill);
                    }
                }
            }

            return ans;
        }

        public string chkLane(string Addr, string Lane)
        {
            string ans = string.Empty;

            if (Addr.Contains("ซ.")||Addr.Contains("ซอย"))
            {
                ans = "";
            }
            else
            {
                if (Lane.Contains("ซ.") || Lane.Contains("ซอย"))
                {
                    ans = string.Format(" {0}",Lane);
                }
                else
                {
                    ans = string.Format(" ซ.{0}", Lane);
                }
            }

            return ans;
        }

        public string chkRoad(string Addr, string Road)
        {
            string ans = string.Empty;

            if (Addr.Contains("ถ.") || Addr.Contains("ถนน"))
            {
                ans = "";
            }
            else
            {
                if (Road.Contains("ถ.") || Road.Contains("ถนน"))
                {
                    ans = string.Format(" {0}",Road);
                }
                else
                {
                    ans = string.Format(" ถ.{0}", Road);
                }
            }

            return ans;
        }

        public string chkDistrict(string Province, string District)
        {
            string ans = string.Empty;

            if (Province.Contains("กรุงเทพ"))
            {
                if (District.Contains("แขวง"))
                {
                    ans = string.Format(" {0}", District);
                }
                else
                {
                    ans = string.Format(" แขวง{0}", District);
                }
            }
            else
            {
                if (District.Contains("ต.")||District.Contains("ตำบล"))
                {
                    ans = string.Format(" {0}", District);
                }
                else
                {
                    ans = string.Format(" ต.{0}", District);
                }               
            }

            return ans;
        }

        public string chkAmphur(string Province, string Amphur)
        {
            string ans = string.Empty;

            if (Province.Contains("กรุงเทพ"))
            {
                if (Amphur.Contains("เขต"))
                {
                    ans = string.Format(" {0}", Amphur);
                }
                else
                {
                    ans = string.Format(" เขต{0}", Amphur);
                }
            }
            else
            {
                if (Amphur.Contains("อ.") || Amphur.Contains("อำเภอ"))
                {
                    ans = string.Format(" {0}", Amphur);
                }
                else
                {
                    ans = string.Format(" อ.{0}", Amphur);
                }
            }

            return ans;
        }

        public string chkProvince(string Province)
        {
            string ans = string.Empty;

            if (Province.Contains("กรุงเทพ"))
            {
                ans = string.Format(" {0}", Province);               
            }
            else
            {
                if (Province.Contains("จ.") || Province.Contains("จังหวัด"))
                {
                    ans = string.Format(" {0}", Province);
                }
                else
                {
                    ans = string.Format(" จ.{0}", Province);
                }
            }

            return ans;
        }

        #endregion
    }
}
